"""Initial CRM-SaaS migration

Revision ID: 7bb3828fa69a
Revises: 
Create Date: 2025-09-29 23:05:13.758158

"""
# pylint: skip-file
# type: ignore
# pyright: reportAttributeAccessIssue=false, reportGeneralTypeIssues=false
from typing import Sequence, Union

from alembic import op  # type: ignore[import]
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '7bb3828fa69a'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('hashed_password', sa.String(length=255), nullable=False),
    sa.Column('full_name', sa.String(length=255), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_admin', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
    op.create_table('auditlog',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('action', sa.String(length=100), nullable=False),
    sa.Column('payload_digest', sa.Text(), nullable=True),
    sa.Column('extra_data', sa.JSON(), nullable=True),
    sa.Column('actor_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['actor_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_auditlog_action'), 'auditlog', ['action'], unique=False)
    op.create_index(op.f('ix_auditlog_actor_id'), 'auditlog', ['actor_id'], unique=False)
    op.create_index(op.f('ix_auditlog_id'), 'auditlog', ['id'], unique=False)
    op.create_table('importbatch',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('filename', sa.String(length=255), nullable=False),
    sa.Column('channel_hint', sa.String(length=50), nullable=True),
    sa.Column('record_count', sa.Integer(), nullable=False),
    sa.Column('status', sa.Enum('PROCESSING', 'COMPLETED', 'FAILED', name='importstatus'), nullable=False),
    sa.Column('imported_by', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['imported_by'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_importbatch_id'), 'importbatch', ['id'], unique=False)
    op.create_index(op.f('ix_importbatch_imported_by'), 'importbatch', ['imported_by'], unique=False)
    op.create_table('leads',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company', sa.String(length=255), nullable=False),
    sa.Column('scene', sa.String(length=255), nullable=False, comment='CRM use case'),
    sa.Column('source_channel', sa.Enum('DOUYIN', 'WECHAT', 'PHONE', 'OFFLINE', 'OTHER', name='sourcechannel'), nullable=False),
    sa.Column('source_detail', sa.Text(), nullable=True),
    sa.Column('import_batch_id', sa.Uuid(), nullable=False),
    sa.Column('stage', sa.Enum('NEW', 'CONTACTED', 'QUALIFIED', 'PROPOSAL', 'CLOSED_WON', 'CLOSED_LOST', name='leadstage'), nullable=False),
    sa.Column('owner_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['owner_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_leads_company'), 'leads', ['company'], unique=False)
    op.create_index(op.f('ix_leads_id'), 'leads', ['id'], unique=False)
    op.create_index(op.f('ix_leads_import_batch_id'), 'leads', ['import_batch_id'], unique=False)
    op.create_index(op.f('ix_leads_owner_id'), 'leads', ['owner_id'], unique=False)
    op.create_index(op.f('ix_leads_source_channel'), 'leads', ['source_channel'], unique=False)
    op.create_index(op.f('ix_leads_stage'), 'leads', ['stage'], unique=False)
    op.create_table('leadaisummary',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('provider', sa.Enum('QWEN3', name='aiprovider'), nullable=False),
    sa.Column('keywords', sa.JSON(), nullable=True),
    sa.Column('pain_points', sa.JSON(), nullable=True),
    sa.Column('suggested_actions', sa.JSON(), nullable=True),
    sa.Column('contacts_highlight', sa.JSON(), nullable=True),
    sa.Column('raw_prompt', sa.Text(), nullable=False, comment='Audit trail'),
    sa.Column('raw_response', sa.Text(), nullable=False, comment='Audit trail'),
    sa.Column('reviewed', sa.Boolean(), nullable=False),
    sa.Column('lead_id', sa.Integer(), nullable=False),
    sa.Column('reviewer_id', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['lead_id'], ['leads.id'], ),
    sa.ForeignKeyConstraint(['reviewer_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_leadaisummary_id'), 'leadaisummary', ['id'], unique=False)
    op.create_index(op.f('ix_leadaisummary_lead_id'), 'leadaisummary', ['lead_id'], unique=False)
    op.create_index(op.f('ix_leadaisummary_reviewer_id'), 'leadaisummary', ['reviewer_id'], unique=False)
    op.create_table('leadcontact',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=True),
    sa.Column('phone', sa.String(length=50), nullable=True),
    sa.Column('role', sa.String(length=100), nullable=False),
    sa.Column('preferred_channel', sa.Enum('WECHAT', 'PHONE', 'EMAIL', 'OTHER', name='preferredchannel'), nullable=False),
    sa.Column('lead_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['lead_id'], ['leads.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_leadcontact_id'), 'leadcontact', ['id'], unique=False)
    op.create_index(op.f('ix_leadcontact_lead_id'), 'leadcontact', ['lead_id'], unique=False)
    op.create_table('leadnote',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('summary', sa.Text(), nullable=False, comment='Communication summary'),
    sa.Column('expectation', sa.Text(), nullable=True, comment='Customer expectations'),
    sa.Column('attachments', sa.JSON(), nullable=True),
    sa.Column('lead_id', sa.Integer(), nullable=False),
    sa.Column('author_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['author_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['lead_id'], ['leads.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_leadnote_author_id'), 'leadnote', ['author_id'], unique=False)
    op.create_index(op.f('ix_leadnote_id'), 'leadnote', ['id'], unique=False)
    op.create_index(op.f('ix_leadnote_lead_id'), 'leadnote', ['lead_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_leadnote_lead_id'), table_name='leadnote')
    op.drop_index(op.f('ix_leadnote_id'), table_name='leadnote')
    op.drop_index(op.f('ix_leadnote_author_id'), table_name='leadnote')
    op.drop_table('leadnote')
    op.drop_index(op.f('ix_leadcontact_lead_id'), table_name='leadcontact')
    op.drop_index(op.f('ix_leadcontact_id'), table_name='leadcontact')
    op.drop_table('leadcontact')
    op.drop_index(op.f('ix_leadaisummary_reviewer_id'), table_name='leadaisummary')
    op.drop_index(op.f('ix_leadaisummary_lead_id'), table_name='leadaisummary')
    op.drop_index(op.f('ix_leadaisummary_id'), table_name='leadaisummary')
    op.drop_table('leadaisummary')
    op.drop_index(op.f('ix_leads_stage'), table_name='leads')
    op.drop_index(op.f('ix_leads_source_channel'), table_name='leads')
    op.drop_index(op.f('ix_leads_owner_id'), table_name='leads')
    op.drop_index(op.f('ix_leads_import_batch_id'), table_name='leads')
    op.drop_index(op.f('ix_leads_id'), table_name='leads')
    op.drop_index(op.f('ix_leads_company'), table_name='leads')
    op.drop_table('leads')
    op.drop_index(op.f('ix_importbatch_imported_by'), table_name='importbatch')
    op.drop_index(op.f('ix_importbatch_id'), table_name='importbatch')
    op.drop_table('importbatch')
    op.drop_index(op.f('ix_auditlog_id'), table_name='auditlog')
    op.drop_index(op.f('ix_auditlog_actor_id'), table_name='auditlog')
    op.drop_index(op.f('ix_auditlog_action'), table_name='auditlog')
    op.drop_table('auditlog')
    op.drop_index(op.f('ix_users_id'), table_name='users')
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.drop_table('users')
    # ### end Alembic commands ###
